import Vue from 'vue'

import Hom from './views/hom/index.vue'
import Register from './views/register/index.vue'
import Login from './views/login/index.vue'
import VueRouter from 'vue-router'
import Wenzhang from './views/hom/wenzhang.vue'
import Xueyuan from './views/hom/xueyuan.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Hom,
    children: [
      {
        path: 'wenzhang',
        component: Wenzhang,
        meta: {
          needLogin: true
        }
      },
      {
        path: 'xueyuan',
        component: Xueyuan,
        meta: {
          needLogin: true
        }
      },
      {
        path: '',
        component: () => import('./views/hom/zhuye.vue'),
        meta: {
          needLogin: true
        }
      }
    ]
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/login',
    component: Login
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  let use = window.sessionStorage.getItem('user')
  if (to.meta.needLogin && !use) {
    next('/login')
  } else {
    next()
  }
})

export default router
